// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'

import router from './router'

// import 'lib-flexible/flexible'

//jquery
// import 'jquery/dist/jquery.min.js';

//bootstrap
// import 'bootstrap/dist/css/bootstrap.min.css';
// import 'bootstrap/dist/js/bootstrap.min.js';

// import './assets/css/slick.css';
// import './assets/css/animate.css';

// import './assets/js/slick.min.js';
// import './assets/js/toTop.js';
// import './assets/js/pictureset.js';

import './assets/css/fonts/iconfont.css';
import './assets/css/fonts/iconfont.js';

import './common/scss/main.scss';
import VueScroller from 'vue-scroller'
// import './assets/js/main.js';
import BaiduMap from 'vue-baidu-map'

import Vconsole from 'vconsole'

// vuex2-添加到vue身上
Vue.use(Element);
Vue.use(VueScroller)

Vue.config.productionTip = false;



Vue.use(BaiduMap, {
    ak: 'Ox4wMgM9NxjAFg0QYa0uzjgNx8iyS4VG'    //这个地方是官方提供的ak密钥
})

// let vConsole = new Vconsole()

router.onError((error) => {
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);
    const targetPath = router.history.pending.fullPath;
    if (isChunkLoadFailed) {
        router.replace(targetPath);
    }
})

router.beforeEach((to, from, next) => {
    // if (to.matched.some(record => record.meta.requireLogin)) { // 判断该路由是否需要登录权限
    //     if (localStorage.getItem('user_token')) { // 判断当前用户的登录信息loginInfo是否存在
    //         next();
    //     } else {
    //         next({
    //             path: '/login'
    //         });
    //     }
    // } else {
    //     next();
    // }
    //   第一次进入项目
    let token = localStorage.getItem('user_token');
    // if (!token && to.path != '/author/author') {
    //     window.localStorage.setItem('beforeLoginUrl', to.fullPath) // 保存用户进入的url

    //     next('/author/author')
    //     return false
    // }

    // if (to.path == '/author/author' && token) {
    //     // 用户使用后退返回到授权页，则默认回到首页
    //     next('/index')
    //     return false
    // }

    /*localStorage.setItem('user_token', 'a3b4775530a816ea649f1f4bf446df22');*/

    let invite_code = to.query.invite_code;
    console.log('获取invite_code:' + invite_code);
    if (invite_code) {
        localStorage.setItem('invite_code', invite_code)
    }

    next()

})

router.afterEach((to, from) => {
    const u = navigator.userAgent.toLowerCase()
    console.log('userAgent', u);
    if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') return
    console.log('to.path', to.path);
    console.log('global.location.pathname', global.location.pathname);
    if (to.path !== global.location.pathname) {
        location.assign(to.fullPath)
    }
})

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    components: {
        App
    },
    template: '<App/>'
})
